dojo.declare("WmSolr", wm.Application, {
	"disableDirtyEditorTracking": false, 
	"eventDelay": 0, 
	"i18n": false, 
	"isSecurityEnabled": false, 
	"main": "Main", 
	"manageHistory": true, 
	"manageURL": false, 
	"name": "", 
	"phoneGapLoginPage": "Login", 
	"phoneMain": "", 
	"projectSubVersion": "Alpha5", 
	"projectVersion": 1, 
	"studioVersion": "6.5.0.RC1", 
	"tabletMain": "", 
	"theme": "wm_default", 
	"toastPosition": "br", 
	"touchToClickDelay": 500, 
	"touchToRightClickDelay": 1500,
	"widgets": {
		silkIconList: ["wm.ImageList", {"colCount":39,"height":16,"iconCount":90,"url":"lib/images/silkIcons/silk.png","width":16}, {}], 
		varManager: ["wm.Variable", {"type":"AnyData"}, {}]
	},
	_end: 0
});

WmSolr.extend({

    _end: 0
});

eval(wm.load("resources/javascript/ajax-solr/core/Core.js"));
eval(wm.load("resources/javascript/ajax-solr/core/Parameter.js"));
eval(wm.load("resources/javascript/ajax-solr/core/ParameterStore.js"));
eval(wm.load("resources/javascript/ajax-solr/core/AbstractManager.js"));

eval(wm.load("resources/javascript/ajax-solr/core/AbstractWidget.js"));
eval(wm.load("resources/javascript/ajax-solr-dojo/ResultWidget.js"));
eval(wm.load("resources/javascript/ajax-solr-dojo/PagerWidget.js"));

AjaxSolr.theme.prototype.result = function (item, snippet) {
  var output = '<div><h2>' + item.title + '</h2>';
  output += '<p id="links_' + item.id + '"></p>';
  output += '<p>' + snippet + '</p></div>';
  return output;
};

AjaxSolr.theme.prototype.snippet = function (doc) {
  var output = '';
  if (doc.text.length > 300) {
    output += doc.dateline + ' ' + doc.text.substring(0, 300);
    output += '<span style="display:none;">' + doc.text.substring(300);
    output += '</span> <a href="#" class="more">more</a>';
  }
  else {
    output += doc.dateline + ' ' + doc.text;
  }
  return output;
};
/**
 * Appends the given items to the given list, optionally inserting a separator
 * between the items in the list.
 *
 * @param {String} list The list to append items to.
 * @param {Array} items The list of items to append to the list.
 * @param {String} [separator] A string to add between the items.
 * @todo Return HTML rather than modify the DOM directly.
 */
AjaxSolr.theme.prototype.list_items = function (list, items, separator) {
  var listNode = dojo.byId(list);
  dojo.empty(listNode);
//  dojo.html.set(dojo.byId(listNode).innerHTML = '<span style="display:inline;"></span>');
  for (var i = 0, l = items.length; i < l; i++) {
    var li = dojo.create('li');
    if (AjaxSolr.isArray(items[i])) {
      for (var j = 0, m = items[i].length; j < m; j++) {
        if (separator && j > 0) {
          //dojo.place(separator,li,"last");
          li.innerHTML += separator;
        }
        dojo.place(items[i][j],li,"last");
      }
    }
    else {
      if (separator && i > 0) {
        //dojo.place(separator,li,"last");
        li.innerHTML += separator;
      }
      dojo.place(items[i],li,"last");
    }
    dojo.place(li,listNode,"last");
  }
};


// $Id$
/**
 * @see http://wiki.apache.org/solr/SolJSON#JSON_specific_parameters
 * @class Manager
 * @augments AjaxSolr.AbstractManager
 */
AjaxSolr.Manager = AjaxSolr.AbstractManager.extend( /** @lends AjaxSolr.Manager.prototype */ {
    executeRequest: function(servlet, string, handler) {
        var self = this;
        string = string || this.store.string();
        handler = handler ||
        function(data) {
            self.handleResponse(data);
        };
        if (this.proxyUrl) {
            //jQuery.post(this.proxyUrl, { query: string }, handler, 'json');
            dojo.xhrPost({
                url: this.proxyUrl,
                content : {query : string},
                load: handler,
                error: function(error) {
                    console.log("Solr xhr error:", this, error);
                },
                handleAs: 'json'
            });
        }
        else {
            var searchTerms = wm.Page.getPage("Main").textSearchBox.getDisplayValue();
            $.ajax({
              'url': 'http://localhost:8080/solr/select',
              'data': {'wt':'json', 'q':searchTerms, 'hl.fl':'*'},  // query is set by contents of "textSearchBox", highlighting fields is set to all fields
              'success': function(data) {
                    dojo.query("#" + this.target).empty();
                    console.log("SOLR-DOJO: Target emptied");
                    console.log("SOLR-DOJO: looping over results");
                    wm.Page.getPage("Main").textNumberOfSearchResults.setDisplayValue(data.response.docs.length);
                    for (var i = 0, l = data.response.docs.length; i < l; i++) {
            
                        var doc = data.response.docs[i];
                        //dojo.query(this.target).append(AjaxSolr.theme('result', doc, AjaxSolr.theme('snippet', doc)));
                        
                        var snippet = '';
                        if (doc.text.length > 300) {
                            snippet += doc.dateline + ' ' + doc.text.substring(0, 300);
                            snippet += '<span style="display:none;">' + doc.text.substring(300);
                            snippet += '</span> <a href="#" class="more">more</a>';
                        }
                        else {
                            snippet += doc.dateline + ' ' + doc.text;
                        }
                        
                        var output = '<div><h2>' + doc.item + '</h2>';
                        output += '<p id="links_' + doc.id + '"></p>';
                        output += '<p>' + snippet + '</p></div>';
                        
                        wm.Page.getPage("Main").html1.setValue("html", output);
//                        dojo.place(output, wm.Page.getPage("WmSolrPage").html1.target, "last");
                    }

                  },
              'dataType': 'jsonp',
              'jsonp': 'json.wrf'
            });
            
            
            
/*            
            //jQuery.getJSON(this.solrUrl + servlet + '?' + string + '&wt=json&json.wrf=?', {}, handler);
            //http://localhost:8094/WmSolr/solr/select/?q=*%3A*
            var queryurl = this.solrUrl + servlet + '/?' + string + '&wt=json';
            console.log("xhrGet:",queryurl);
            dojo.xhrGet({
//                url: this.solrUrl + servlet + '/?' + string + '&wt=json',
//                url: this.solrUrl + 'select/?' + string + '&wt=json',
                url: 'http://localhost:8080/solr/select/?' + string + '&wt=json',
                //content : {query : string},
                load: handler,
                error: function(error) {
                    console.log("Solr xhr error:", this, error);
                },
                handleAs: 'json'
            });
*/        
        }
    }
});